home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4653 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: lrz-muenchen.de!sun2!ua302aa
  2. From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Watcom: pad a string ?
  5. Date: 4 Feb 1996 14:30:07 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4f2ftf$rjb@sparcserver.lrz-muenchen.de>
  9. References: <ZCOExQTC3kJT089yn@pinerolo.gvo.it>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12.  
  13. albe@pinerolo.gvo.it (Alberto Velo) writes:
  14.  
  15. >Is there a function to pad a string with a charachter, using Watcom C/C++ 10.5 
  16. >?
  17.  
  18. Maybe, but only the documentation that comes with that implementation of
  19. the C programming language will tell you for sure.
  20.  
  21. >I have an array, part of a structure (i.e. msg.from="Myname"); since array is 
  22. >of defined length in the structure ( char from[36] ), I'd need to let msg.from 
  23. >36 chars length, padding it with spaces.
  24.  
  25. Why do you think so? As long as "msg.from" indeed _is_ a string, i.e. if
  26. it is '\0'-terminated, there is no need to pad it with spaces. If you 
  27. are dealing with an interface to a system that needs padding, write your
  28. own padding function or look for an implementation dependend solution.
  29. The functions memset() and strncpy() from the standard C library might
  30. be helpful when writing you own padding function.
  31.  
  32. You could try something like:
  33.  
  34.    #include <string.h>
  35.  
  36.    char *
  37.    pad_str(char *str, int pad, char *tgt, size_t sz)
  38.    {
  39.       size_t len = strlen(str);
  40.       memset(tgt, pad, sz - 1);
  41.       tgt[sz - 1] = '\0';
  42.       return strncpy(tgt, str, len < sz - 1 ? len : sz - 1);
  43.    }
  44.  
  45. where "str" is the string you want to insert, i.e. "Myname" in your 
  46. example, "pad" is the padding character you want to use, i.e. ' '
  47. in your example, "tgt" is a target buffer for the padded string,
  48. i.e "msg.from" in your example, and "sz" is the size of the
  49. target buffer in bytes.
  50.  
  51. >Else I need to know how to write the structure to a binary file: if I use a 
  52. >sizeof(structname), while char arrays are shorter than the ones defined in the 
  53. >struct, I find additional garbage chars in the file, who fill the array sizes.
  54.  
  55. So what? You want to write the whole structure, you write it. Those
  56. "additional garbare chars" are in the struct, and they appear in the
  57. file. If you want to avoid storing "garbage" chars, you cannot use
  58. arrays of characters with a fixed size.
  59.  
  60. >For example, I fill msg.from with "Myname"; from is defined as 3 chars array, 
  61. >in the struct: when I write the entire struct to the file, the from field 
  62. >appears to be "Myname   @ # *
  63.  3 ..." , that is "Myname" + extra garbage for a 
  64. >total of 36 chars.
  65.  
  66. How exactly do you fill "msg.from", i.e. do you copy the terminating
  67. '\0'? I dare say not :-). BTW, is "msg.from" an array of 3 characters
  68. or an array of 36 characters? In the first case, your problem is that
  69. "Myname" does not fit into an array of 3 characters.
  70.  
  71. Kurt
  72. --
  73. | Kurt Watzka                             Phone : +49-89-2180-6254
  74. | watzka@stat.uni-muenchen.de
  75. | ua302aa@sunmail.lrz-muenchen.de
  76.